package com.nuc.bigdata.model;

import java.io.Serializable;

/**
 * 返回结果的统一封装类
 * @author JackLuo
 * @date 2019/10/30
 */
public class Result implements Serializable {

    /**
     * 结果编码(各种结果对应的状态码)
     */
    private Integer code;

    /**
     * 结果信息描述
     */
    private String message;

    /**
     * 实际接口的数据
     */
    private Object data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 结果类的构造方法，传入的ResultCode类是自定义枚举类
     * 枚举类中包含了结果编码和结果信息描述
     * 这里就用枚举类的get方法来给Result赋值
     * @param resultCode 枚举类
     * @param data 实际接口数据
     */
    public Result(ResultCode resultCode, Object data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    /**
     * 只传入了结果编码和结果信息描述
     * @param resultCode 枚举类
     */
    public Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 定义静态方法是为了简化其他类中为了包装接口结果对象
     * 如果不使用静态方法，需要使用构造方法，再返回，不美观(就是为了美观)
     * @param data 接口数据
     * @return 统一结果类对象
     */
    public static Result success(Object data){
        return new Result(ResultCode.SUCCESS,data);
    }

    public static Result success(){
        return new Result(ResultCode.SUCCESS);
    }


    public static Result failure(ResultCode resultCode,Object data){
        return new Result(resultCode,data);
    }

    public static Result failure(ResultCode resultCode){
        return new Result(resultCode);
    }

    public static Result failure(Integer code,String message,Object data){
        return new Result(code, message, data);
    }
}
